package com.brainsitter;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;

import com.brainsitter.db.DBHandler;
import com.brainsitter.db.tables.Note;
import com.brainsitter.db.tables.Task;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NoteTabActivity extends ListActivity {
	
	private ArrayList<Note> notes = null;
	private ModifiedAdapter madapter;
	private DBHandler db;
	
	ImageView noteMenuImage;
	
	Button saveAddNoteButton;
	Button cancelAddNoteButton;
	
	EditText addTitle;
	EditText addContent;
	
	Dialog dialog;
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.print);
        db = new DBHandler(this);
        notes = db.getAllNotes();
        Log.d("Notes:", String.valueOf(notes.size()));
        madapter = new ModifiedAdapter(this, R.layout.note_tab, notes);
        setListAdapter(madapter);
        registerForContextMenu(getListView());
        noteMenuImage = (ImageView) findViewById(R.id.noteMenuImage);
        noteMenuImage.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				noteMenuOnClick(v);
			}
		});
        
	}
	
	

	protected void noteMenuOnClick(View v) {
		// TODO Auto-generated method stub
		dialog = new Dialog(this);
		dialog.setContentView(R.layout.add_note_form_dialog);
		LayoutParams lp = dialog.getWindow().getAttributes();    
        //lp.x=150;
        lp.y=90;
        lp.gravity=Gravity.TOP | Gravity.RIGHT;
        lp.dimAmount= 0.5f;      
        dialog.setTitle("Add Note");
        dialog.show();
        
        saveAddNoteButton = (Button) dialog.findViewById(R.id.saveAddNoteButton);
    	cancelAddNoteButton = (Button) dialog.findViewById(R.id.cancelAddNoteButton);
    	
    	addTitle = (EditText) dialog.findViewById(R.id.addTitle);
    	addContent = (EditText) dialog.findViewById(R.id.addContent);

    	saveAddNoteButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveAddNoteButtonOnClick(v);
			}
		});
    	

    	cancelAddNoteButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cancelAddNoteButtonOnClick(v);
			}
		});
    	
	}



	protected void saveAddNoteButtonOnClick(View v) {
		// TODO Auto-generated method stub
		if(!addTitle.getText().toString().isEmpty()){
			Note note = new Note();
			note.setNoteName(addTitle.getText().toString());
			note.setContent(addContent.getText().toString());
			Calendar c = Calendar.getInstance();
			note.setDateCreated(new Date(c.get(Calendar.YEAR) , c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)));
			note.setTimeCreated(new Time(c.get(Calendar.HOUR), c.get(Calendar.MINUTE), c.get(Calendar.SECOND)));
			db.addNote(note);
			dialog.dismiss();
			onResume();
		}
	}



	protected void cancelAddNoteButtonOnClick(View v) {
		// TODO Auto-generated method stub
		dialog.dismiss();
	}



	public void onResume(){
		super.onResume();
        notes = db.getAllNotes();
        madapter = new ModifiedAdapter(this, R.layout.note_tab, notes);
        setListAdapter(madapter);
        Log.d("Notes:", String.valueOf(notes.size()));
	}
	
	private class ModifiedAdapter extends ArrayAdapter<Note> {

        private ArrayList<Note> items;

        //constructor
        public ModifiedAdapter(Context context, int textViewResourceId, ArrayList<Note> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        //populate the list following the format specified in R.layout.row
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.note_tab, null);
                }
                Note n = items.get(position);
                if (n != null) {
                        TextView tt = (TextView) v.findViewById(R.id.text);
                        if (tt != null) {
                              tt.setText(""+n.getNoteName()); 
                        }
                }
                return v;
        }
        
	}
	
	
	
	@Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);  
    	Log.d("VIEW ID", String.valueOf(v.getId()));
        menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "Edit");  
        menu.add(0, v.getId(), 0, "Delete");  
    }
	
	@Override  
    public boolean onContextItemSelected(MenuItem item) {  
    	
    	AdapterView.AdapterContextMenuInfo info = null;
    	try{
    		info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    	}
    	catch(ClassCastException e){
    		Log.e("", "bad menu info",e);
    	}
    	int id = (int)getListAdapter().getItemId(info.position);
    	Log.d("", "id = "+id);
    	
    	
        if(item.getTitle()=="Edit")
        {
        	edit(id);
        }  
        else if(item.getTitle()=="Delete"){
        	delete(id);
        }  
        else {return false;}  
    return true;  
    }  
  
    public void edit(int id){  
        Toast.makeText(this, "Edit called ", Toast.LENGTH_SHORT).show();  
        //Note n = madapter.getItem(id);
		//Intent intent = new Intent().setClass(this, EditNote.class);
		//startActivity(intent);
        //EditNote.idNum = n.getNoteId();
    }  
    public void delete(int id){   
        Note n = madapter.getItem(id);
        db.deleteNote(n);
        onResume();
    }  
}
